<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
        这个表达式实际上就是后缀表达式。遇到符号，就弹出两个元素计算，重新入栈
        如果是数字，就直接入栈。就是要注意减和除。
        重点！！！ typeof NaN=='number' 只能通过isNaN来判断是否是符号喽~~
        比如实现计算器，其实就可以把中缀表达式转为后缀，然后用该方式计算

        */
      var evalRPN = function (tokens) {
        const stack = []
        for (let i = 0; i < tokens.length; i++) {
          let m = Number(tokens[i])
          if (!isNaN(m)) {
            stack.push(m)
          } else {
            let a = Number(stack.pop())
            let b = Number(stack.pop())
            switch (tokens[i]) {
              case '+':
                let sum = a + b
                stack.push(sum)
                break
              case '-':
                let sub = a - b
                stack.push(sub)
                break
              case '*':
                let chen = a * b
                stack.push(chen)
                break
              case '/':
                debugger
                let chu = parseInt(b / a)
                stack.push(chu)
                break
            }
          }
        }
        return stack[0]
      }
      console.log(evalRPN(['4', '13', '5', '/', '+']))
    </script>
  </body>
</html>
